// pages/goods_list/index.js
import {request} from '../../utils/request.js';
Page({
    /**
     * 页面的初始数据
     */
     data: {
        itemTabs:[
            "综合",
            "价格",
            "销量"
        ],
        currentSelectItem:0,
        goods:[],
        currentPage:1,
        load:false,
        end:false
    },
    cid:'',
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        this.cid = options.cat_id;
        this.loadProductList();
    },

    handlerTabChange:function(e){
        console.log(e);
        let {tabitem} = e.target.dataset;

        this.setData({
            currentSelectItem:tabitem
        });
    },

    loadProductList:function(){
        console.log("触底需要加载 新数据......"+this.data.currentPage);
        request({
            method:'GET',
            url:'/goods/search',
            data:{
                pagenum:this.data.currentPage,
                cid:this.cid
            }
        }).then(goodsData=>{
            console.log(goodsData);
            let tatal  = goodsData.total;
            let goods  = goodsData.goods;
            console.log(goods);
            let lastGoods = this.data.goods;
            // lastGoods.push
            goods.forEach(e=>{
                lastGoods.push(e);
            })
            this.setData({goods:lastGoods});
            this.setData({
                currentPage:this.data.currentPage+1
            });

            if(tatal<=this.data.goods.length){
                this.setData({
                    end:true
                })
            }

        })
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function () {
       
       if( this.data.end){
        wx.showToast({
            title: '已经加载完数据源了...',
            icon: 'success',
            duration: 2000
        })
          
          
       } 

       if(this.data.load || this.data.end){
            return;
       }

       this.setData({
            load:true
       }) 
       this.loadProductList();
    },
    handleGoodsTap:function(e){
        let {goodsid} = e.currentTarget.dataset;
        console.log("产品ID:"+goodsid);
        wx.redirectTo({
          url: '/pages/goods_detail/index?id='+goodsid,
        })
    }
    
})
